package com.appkubes.mall.restful.application.user.hierarchy.service.impl;

import com.appkubes.common.exception.BusinessException;
import com.appkubes.mall.core.entity.MallPromoter;
import com.appkubes.mall.core.entity.MallUser;
import com.appkubes.mall.core.entity.MallUserHierarchy;
import com.appkubes.mall.core.service.BaseMallPromoterService;
import com.appkubes.mall.core.service.BaseMallUserHierarchyService;
import com.appkubes.mall.core.service.impl.BaseMallUserHierarchyServiceImpl;
import com.appkubes.mall.restful.application.promoter.annotation.UserRegisterEvent;
import com.appkubes.mall.restful.application.promoter.entity.eo.UserRegisterEO;
import com.appkubes.mall.restful.application.promoter.service.PromoterService;
import com.appkubes.mall.restful.application.user.account.history.service.UserAccountHistoryService;
import com.appkubes.mall.restful.application.user.hierarchy.dao.UserHierarchyDao;
import com.appkubes.mall.restful.application.user.hierarchy.entity.dto.UserHierarchyDTO;
import com.appkubes.mall.restful.application.user.hierarchy.entity.vo.UserHierarchyGetVO;
import com.appkubes.mall.restful.application.user.hierarchy.service.UserHierarchyService;
import com.appkubes.mall.restful.application.user.user.entity.vo.UserRegisterVO;
import com.appkubes.mall.restful.application.user.user.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class UserHierarchyServiceImpl extends BaseMallUserHierarchyServiceImpl implements UserHierarchyService {

    @Resource
    UserService userService;
    @Resource
    PromoterService promoterService; //推广员模块
    @Resource
    BaseMallUserHierarchyService userHierarchyService;
    @Resource
    UserHierarchyDao userHierarchyDao;

    @Value("${kylin.plugin.hierarchy.lev}")
    private int HIERACHY_LEV;

    @Override
    public Boolean register(String userKey, UserRegisterVO registerVO) throws BusinessException {
        log.info("register:{}", userKey);
        MallUserHierarchy userHierarchy = new MallUserHierarchy();
        String promoterKey = registerVO.getPromoterKey();
        String parentUserKey = registerVO.getInvitorKey();

        //记录用户上下级
        if (parentUserKey != null && !"".equalsIgnoreCase(parentUserKey)) {
            setHierarchyRelation(userHierarchy, parentUserKey);

            MallUserHierarchy parentUserHierarchy = userHierarchyService.getById(parentUserKey);
            if (parentUserHierarchy != null) {
                //父用户的推广人继承到下级用户
                userHierarchy.setPromoterKey(parentUserHierarchy.getPromoterKey());
            }
        }

        //记录推广员
        if (promoterKey != null && !"".equalsIgnoreCase(promoterKey)) {
            MallPromoter promoter = promoterService.getById(promoterKey);
            if (promoter != null) {
                userHierarchy.setPromoterKey(registerVO.getPromoterKey());
            }

        }

        // aop统计推广员注册数量
        promoterService.statisUserReg(userKey, userHierarchy.getPromoterKey());

        userHierarchy.setUserKey(userKey);
        return this.saveOrUpdate(userHierarchy);
    }

    // 通过配置项，动态约束用户的上级关系
    private void setHierarchyRelation(MallUserHierarchy mallUserHierarchy, String parentUserKey) {
        if (parentUserKey == null) return;

        // 设置一级经纪人
        mallUserHierarchy.setParentUserKey(parentUserKey);

        // 根据层级约束，动态设置上级
        MallUserHierarchy parentHierarchy = userHierarchyService.getById(parentUserKey);
        if (parentHierarchy != null) {
            if (HIERACHY_LEV >= 2) {
                mallUserHierarchy.setParentUserKey2(parentHierarchy.getParentUserKey());
            }
            if (HIERACHY_LEV >= 3) {
                mallUserHierarchy.setParentUserKey3(parentHierarchy.getParentUserKey2());
            }
            if (HIERACHY_LEV >= 4) {
                mallUserHierarchy.setParentUserKey4(parentHierarchy.getParentUserKey3());
            }
            if (HIERACHY_LEV >= 5) {
                mallUserHierarchy.setParentUserKey5(parentHierarchy.getParentUserKey4());
            }
        }
    }

    /**
     * 获取用户的推广下级用户信息
     *
     * @param hierarchyGetVO
     * @return
     */
    @Override
    public IPage<UserHierarchyDTO> getListByParentKey(UserHierarchyGetVO hierarchyGetVO) {
        String userKey = hierarchyGetVO.getUserKey();
        IPage<MallUserHierarchy> page = new Page<>(hierarchyGetVO.getPage(), hierarchyGetVO.getSize());
        IPage<UserHierarchyDTO> hierarchies = userHierarchyDao.getListByParentKey(page, userKey);
        return hierarchies;
    }

    /**
     * 获取用户的二级推广用户信息
     *
     * @param hierarchyGetVO
     * @return
     */
    @Override
    public IPage<UserHierarchyDTO> getListByParentKey2(UserHierarchyGetVO hierarchyGetVO) {
        String userKey = hierarchyGetVO.getUserKey();
        IPage<MallUserHierarchy> page = new Page<>(hierarchyGetVO.getPage(), hierarchyGetVO.getSize());
        IPage<UserHierarchyDTO> hierarchies = userHierarchyDao.getListByParentKey2(page, userKey);
        return hierarchies;
    }

    //获取上级用户信息
    @Override
    public MallUser getParenUser(String userKey) {
        MallUserHierarchy mallUserHierarchy = this.getById(userKey);
        if (mallUserHierarchy != null) {
            return userService.getById(mallUserHierarchy.getParentUserKey());
        }

        return null;
    }

}
